//
//  RSImagePicker.m
//  StaffTax
//
//  Created by 侯伟 on 2020/2/26.
//  Copyright © 2020 侯伟. All rights reserved.
//

#import "RSImagePicker.h"

typedef void(^ImageBlock)(UIImage* image);

@interface RSImagePicker ()<UIImagePickerControllerDelegate,UINavigationControllerDelegate>

@property(nonatomic,copy)ImageBlock block;
@property(nonatomic,assign)BOOL isEdit;

@end

@implementation RSImagePicker

+(instancetype)shareInstance{
    static RSImagePicker* imagePicker;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        imagePicker = [RSImagePicker new];
    });
    return imagePicker;
}

- (void)imagePickerIsEdit:(BOOL)edit block:(ImageBlock)block{
    self.block = block;
    self.isEdit = edit;
    UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"图片选择" message:NULL preferredStyle:UIAlertControllerStyleActionSheet];
    UIAlertAction* camera = [UIAlertAction actionWithTitle:@"相机" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
        if(![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]){
            [Toast toast:@"相机不可用"];
            return;
        }
        UIImagePickerController* imagePicker = [UIImagePickerController new];
        imagePicker.delegate = self;
        imagePicker.allowsEditing = edit;
        imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
        [[UIViewController topViewController] presentViewController:imagePicker animated:YES completion:NULL];
    }];
    
    UIAlertAction* photoLibrary = [UIAlertAction actionWithTitle:@"相册" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
        if(![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]){
            [Toast toast:@"相册不可用"];
            return ;
        }
        
        UIImagePickerController* imagePicker = [UIImagePickerController new];
        imagePicker.delegate = self;
        imagePicker.allowsEditing = edit;
        imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        [[UIViewController topViewController] presentViewController:imagePicker animated:YES completion:NULL];
    }];
    
    UIAlertAction* canel = [UIAlertAction actionWithTitle:@"取消" style:(UIAlertActionStyleCancel) handler:NULL];
    
    [alert addAction:camera];
    [alert addAction:photoLibrary];
    [alert addAction:canel];
    
    [[UIViewController topViewController] presentViewController:alert animated:YES completion:NULL];
}

#pragma mark -- UIImagePickerControllerDelegate

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<UIImagePickerControllerInfoKey, id> *)info{
    
    [picker dismissViewControllerAnimated:YES completion:nil];
    
    UIImage* image;
    if(self.isEdit){
        image = info[UIImagePickerControllerEditedImage];
    }else{
        image = info[UIImagePickerControllerOriginalImage];
    }
    if(self.block){
        self.block(image);
    }
}

@end
